﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UCL.Forms;

namespace DBKit5.UControls
{
    /// <summary>
    /// 连接对象选择器
    /// </summary>
    /// <remarks>2014-3-19 by sutroon</remarks>
    public partial class ucConnectionSelector : UserControl
    {
        /// <summary>
        /// 获取或设置是否显示复选框
        /// </summary>
        public bool CheckBoxes { get { return tvGroup.CheckBoxes; } set { tvGroup.CheckBoxes = value; } }

        /// <summary>
        /// 获取选中的数量
        /// </summary>
        public int CheckedItemsCount { get { return tvGroup.GetCheckedItemsCount(); } }

        /// <summary>
        /// 获取或设置选中项目改变时的委托方法
        /// </summary>
        public Action<TreeNode> AfterSelectHandler { get; set; }

        // 数据源缓存
        private DataTable dtSource;
        private string xmlSource;
        public ucConnectionSelector()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="nameId"></param>
        /// <param name="textId"></param>
        public void LoadData(DataTable dt, string nameId = "ID", string textId = "Name")
        {
            if ((dt == null || dt.Rows.Count == 0) && dtSource == null) return;
            if (dt != null)
            {
                StringBuilder sbMsg = new StringBuilder();
                if (dt.Columns.Contains(nameId)) sbMsg.AppendFormat("数据源不包含列[{0}].{1}", nameId, Environment.NewLine);
                if (dt.Columns.Contains(textId)) sbMsg.AppendFormat("数据源不包含列[{0}].{1}", textId, Environment.NewLine);
                if (sbMsg.Length > 0)
                {
                    MessageBox.Show(sbMsg.ToString());
                    return;
                }
                dtSource = dt;
            }
            tvGroup.Nodes.Clear();
            foreach (DataRow r in dtSource.Rows)
            {
                tvGroup.Nodes.Add(r[nameId].ToString(), r[textId].ToString());
            }
            tvGroup.ExpandAll();
        }

        /// <summary>
        /// 加载数据
        /// <para>如：base.ExecuteTreeXml(string.Format("select ID, Name from tGeneralCatalog where ParentID='{{0}}' and ExTags='{0}'", exTags), parentId, "Select ID, Name from tConnections where GroupID='{0}'");</para>
        /// </summary>
        /// <param name="xml"></param>
        public void LoadData(string xml)
        {
            if (string.IsNullOrEmpty(xml)) return;
            xmlSource = xml;
            tvGroup.Nodes.Clear();
            tvGroup.LoadXmlString(xml);
            tvGroup.ExpandAll();
        }

        private void tvGroup_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (AfterSelectHandler != null && tvGroup.SelectedNode != null) AfterSelectHandler(tvGroup.SelectedNode);
        }

        /// <summary>
        /// 获取选中的值的列表
        /// <para>如：tv.GetCheckedValues(a => a.Name);</para>
        /// </summary>
        /// <param name="func"></param>
        /// <returns></returns>
        public IList<string> GetCheckedValues(Func<TreeNode, string> func = null)
        {
            return tvGroup.GetCheckedValues(func == null ? n => n.Name : func);
        }
    }
}
